﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yes.Application.Category.Dto;
using Yes.Core.Extensions;
using Yes.Domain;
using Yes.Domain.Entity;
using Yes.Domain.Enum;

namespace Yes.Application.Category
{

    public class CategoryService : BaseService, ICategoryService
    {
        private IRepository<CategoryEntity> _categoryRepository;
        private IRepository<ArticleEntity> _articleRepository;

        public CategoryService(IRepository<CategoryEntity> categoryRepository, IRepository<ArticleEntity> articleRepository)
        {
            _categoryRepository = categoryRepository;
            _articleRepository = articleRepository;
        }

        public List<CategoryEntity> GetCategoryList()
        {
            return _categoryRepository.TableNoTracking.OrderByDescending(p=>p.SortId).ToList();
        }

        public CategoryEntity GetCategory(int id)
        {
            return _categoryRepository.Get(id);
        }

        public ResultDto AddCategory(CategoryAddDto dto)
        {
            var category = dto.MapTo<CategoryEntity>();
            if (_categoryRepository.Count(p => p.Name == category.Name&& p.Id!= category.Id) > 0)
            {
                return new ResultDto
                {
                    Status = false,
                    ErrorMessage = "分类名重复："+ category.Name
                };
            }
            if (category.Id == 0)
            {
                _categoryRepository.Insert(category);
            }
            else
            {
                category.ArticleCount = _articleRepository.Count(x => x.CategoryId == category.Id && x.Status== ArticleStatusEnum.Normal);
                _categoryRepository.Update(category);
            }

            return new ResultDto
            {
                Status = true
            };
        }

        public ResultDto DeleteCategory(List<int> ids)
        {
            var categorys = _categoryRepository.Query(p => ids.Contains(p.Id)).ToList();
            var failedCategorys = categorys.Where(category => _articleRepository.Count(p => p.CategoryId == category.Id) > 0).ToList();//删除失败的分类

            ids.RemoveAll(p => failedCategorys.Select(x => x.Id).Contains(p));
            if (ids.Count > 0)
            {
                _categoryRepository.Delete(p => ids.Contains(p.Id));
            }
            

            return new ResultDto
            {
                Status = true,
                ErrorMessage = failedCategorys.Count > 0 ? "分类[" +   string.Join(',', failedCategorys.Select(p => p.Name)) + "]下还有文章，删除失败" : ""
            };
        }


        public ResultDto UpdateOrder(List<int> ids, List<int> sortids)
        {
            var categorys = _categoryRepository.Query(p => ids.Contains(p.Id)).ToList();

            for (int i = 0; i < ids.Count; i++)
            {
                int id = ids[i];
                var category = categorys.FirstOrDefault(p=>p.Id== id);
                if (category != null)
                {
                    category.SortId = sortids[i];
                }
                
            }
            _categoryRepository.Update(categorys);

            return new ResultDto
            {
                Status = true
            };
        }
    }
}
